package com.goblin;

import java.util.concurrent.ThreadPoolExecutor;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;

@EntityScan({ "com.goblin.model.uaa.po",
		"com.goblin.model.film.po",
		"com.goblin.model.media.po" })
@EnableJpaRepositories(basePackages = { "com.goblin.model.uaa.repository",
		"com.goblin.model.film.repository",
		"com.goblin.model.media.repository" })

@SpringBootApplication
public class AppBoot {

	public static void main(String[] args) {
		SpringApplication.run(AppBoot.class, args);
	}

	@Bean
	public RestTemplate restTemplate(RestTemplateBuilder builder) {
		return builder.build();

	}

	@Bean
	public ThreadPoolTaskExecutor defaultThreadPool() {
		ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
		// 核心线程数目
		executor.setCorePoolSize(16);
		// 指定最大线程数
		executor.setMaxPoolSize(64);
		// 队列中最大的数目
		executor.setQueueCapacity(16);
		// 线程名称前缀
		executor.setThreadNamePrefix("defaultThreadPool_");
		//rejection-policy：当pool已经达到max size的时候，如何处理新任务
		//CALLER_RUNS：不在新线程中执行任务，而是由调用者所在的线程来执行
		//对拒绝task的处理策略
		executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
		//线程空闲后的最大存活时间
		executor.setKeepAliveSeconds(60);
		//加载
		executor.initialize();
		return executor;
	}

	@Bean
	public ServerEndpointExporter serverEndpointExporter() {
		return new ServerEndpointExporter();
	}

}
